package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.AuthDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserRealNameMapper;
import com.heima.user.service.AdUserAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
@Transactional
@Slf4j
public class AdUserAuthServiceImpl extends ServiceImpl<ApUserRealNameMapper, ApUserRealname> implements AdUserAuthService {

    @Autowired
    private ApUserRealNameMapper apUserRealNameMapper;

    /**
     * 查询用户审核列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(AuthDto dto) {

        //检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //分页参数检查
        dto.checkParam();
        //分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());

        //条件构造器
        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据状态查询
        lambdaQueryWrapper.eq(dto.getStatus() != null, ApUserRealname::getStatus, dto.getStatus());
        //按审核时间升序排序
        lambdaQueryWrapper.orderByAsc(ApUserRealname::getCreatedTime);
        apUserRealNameMapper.selectPage(page, lambdaQueryWrapper);
        //返回结果
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());

        return responseResult;
    }

    /**
     * 审核失败
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto dto) {

        if (dto != null && dto.getId() != null) {
            //查询数据
            ApUserRealname apUserRealname = apUserRealNameMapper.selectById(dto.getId());
            if (apUserRealname != null) {
                apUserRealname.setStatus((short) 2);
                apUserRealname.setSubmitedTime(new Date());
                apUserRealname.setUpdatedTime(new Date());
                apUserRealname.setReason(dto.getMsg());
                //更新数据
                apUserRealNameMapper.updateById(apUserRealname);
            }
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    /**
     * 审核通过
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authPass(AuthDto dto) {
        if (dto != null && dto.getId() != null) {
            //查询数据
            ApUserRealname apUserRealname = apUserRealNameMapper.selectById(dto.getId());
            if (apUserRealname != null) {
                apUserRealname.setStatus((short) 9);
                apUserRealname.setSubmitedTime(new Date());
                apUserRealname.setUpdatedTime(new Date());
                //更新数据
                apUserRealNameMapper.updateById(apUserRealname);
            }
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
}
